#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <set>

using namespace std;

  struct TreeNode {
      int val;
      TreeNode *left;
      TreeNode *right;
      TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  };

class Solution {
public:
    vector<vector<int> > levelOrder(TreeNode *root) {
        int curLevel = 1, nextLevel = 0;
        vector<vector<int> > ans;
        vector<int> cur;
        queue<TreeNode *> myQue;
        if (!root) return ans;
        myQue.push(root);
        while(!myQue.empty()) {
	    TreeNode *front = myQue.front();
	    myQue.pop();
	    cur.push_back(front->val);
            if (front->left) { myQue.push(front->left); nextLevel++;}
	    if (front->right) {myQue.push(front->right); nextLevel++;}
            curLevel--;
	    if (curLevel == 0) {
		curLevel = nextLevel;
		ans.push_back(cur);
		nextLevel = 0;
		cur.clear();
	    }
        }
	return ans;
    }
};
//pass all the test
int main(int argc, char **argv)
{

}
